﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
	using UFIDA.U9.Base.UOM;
	using UFIDA.U9.CBO.MFG.Routing;
	using UFIDA.U9.CBO.MFG.WorkCenter;
	using UFIDA.U9.CBO.Pub.Controller;
	using UFIDA.U9.MO.MO;
	using UFSoft.UBF.AopFrame;
	using UFSoft.UBF.Business;
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// MOOperationModifySV partial 
	/// </summary>	
	public partial class MOOperationModifySV 
	{	
		internal BaseStrategy Select()
		{
			return new MOOperationModifySVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class MOOperationModifySVImpementStrategy : BaseStrategy
	{
		public MOOperationModifySVImpementStrategy() { }

		public override object Do(object obj)
		{						
			MOOperationModifySV bpObj = (MOOperationModifySV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");
				List<CopyOfCommonArchiveDataDTOData> retlist = new List<CopyOfCommonArchiveDataDTOData>();
				foreach (var dto in bpObj.Data)
				{
					MO mo;
					mo = MO.Finder.Find($"DocNo='{dto.DocNo}'");
					if (mo == null)
					{
						throw new Exception($"单号:[{dto.DocNo}]不存在!");
					}
					else
					{
						ModifyMOOperation(mo, dto);
					}

					retlist.Add(new CopyOfCommonArchiveDataDTOData()
					{
						ID = mo.ID,
						Code = mo.DocNo
					});
				}

				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{

				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}

		private void ModifyMOOperation(MO mo, MOAITDTOData dto)
        {
			if (dto.MOOperations == null)
				throw new Exception("MOOperations不能为空!");

			//构造DTO行缓存
            Dictionary<String, MOOperationAITDTOData> dtolinedic = new Dictionary<string, MOOperationAITDTOData>();
            foreach (var dtoline in dto.MOOperations)
            {
                if (!dtolinedic.ContainsKey(dtoline.OperationNum))
                {
                    dtolinedic.Add(dtoline.OperationNum, dtoline);
                }
            }
            //构造实体行缓存
            Dictionary<String, MOOperation> linedic = new Dictionary<string, MOOperation>();
            foreach (var line in mo.MOOperations)
            {
                if (!linedic.ContainsKey(line.OperationNum))
                {
                    linedic.Add(line.OperationNum, line);
                }
            }
            using (Session s = Session.Open())
			{
                //逐行修改或删除行
                List<MOOperation> dellist = new List<MOOperation>();
                foreach (var line in mo.MOOperations)
                {
                    if (dtolinedic.ContainsKey(line.OperationNum))
                    {
                        //修改行
                        SetLineValues(line, dtolinedic[line.OperationNum], mo);
                    }
                    else
                    {
                        //删除行
                        //这里不能line.Remove(),会导致集合发生变化,报错
                        dellist.Add(line);
                    }
                }
                foreach (var line in dellist)
                {
                    line.Remove();
                }

                //新增行
                foreach (var dtoline in dto.MOOperations)
                {
                    //循环DTO行
                    if (!linedic.ContainsKey(dtoline.OperationNum))
                    {
                        //新增行
                        MOOperation line = MOOperation.Create(mo);
                        SetLineValues(line, dtoline, mo);
                    }
                }
				//生产订单工序按照工序号排序
                mo.SortOperationAsc();
                s.Commit();
            }
		}

        private void SetLineValues(MOOperation line, MOOperationAITDTOData dtoline, MO mo)
        {
			line.WorkCenter = EntityUtils.GetEntityByCode<WorkCenter>(dtoline.WorkCenter);
			line.TimeUOM = EntityUtils.GetEntityByCode<UOM>(dtoline.TimeUOM);
			if (dtoline.StdOp != null)
			{
				line.StdOp = EntityUtils.GetEntityByCode<StdOperation>(dtoline.StdOp.Code);
				line.TimeUOM = line.StdOp.TimeUOM;
			}
			line.OperationNum=dtoline.OperationNum;
			line.OpDescription=dtoline.OpDescription;

            //默认值赋值
            line.PlanProductQty = mo.ProductQty;
            line.PlanStartDate = mo.StartDate;
			line.PlanCompleteDate=mo.CompleteDate;
            line.StartTimeUOM = line.TimeUOM;
			line.CompleteTimeUOM = line.TimeUOM;
            line.OutputUOMRatio2PU = 1;
			line.OutputUOMRatio2Base = 1;
			line.OutputUOM = mo.ProductUOM;
			line.OutputBaseUOM = mo.ProductBaseUOM;
			line.OpOrg = mo.Org;
			line.IsRcvDirectly = true;
			line.IsCountPoint = true;
			line.IsBackPushOtherRes = true;
			line.IsBackPushMachine = true;
			line.IsBackPushLabor = true;
			line.IsBackPushItem = true;
			line.IsAutoCalcCost = true;

        }
    }

	#endregion
	
	
}